package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.bean.cartoon.CartoonDetail;
import com.taptech.doufu.bean.cartoon.CartoonSectionInfo;
import com.taptech.doufu.bean.cartoon.WholeCartoonDetaisBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.services.CartoonServices;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by longsiyang on 15/12/11.
 */
public class CartoonEpisodeMenuAdapter extends RecyclerView.Adapter<CartoonEpisodeMenuAdapter.CartoonEpisodeItemViewHolder> {

    Context mContext;
    private int currentPos = -1;
    WholeCartoonDetaisBean bean;

    public boolean orderSec = true;

    public CartoonEpisodeMenuAdapter(Context context , WholeCartoonDetaisBean bean){
        this.bean = bean;
        this.mContext = context;
    }

    /**
     * 获取章节id
     * @return
     */
    private String getChapterId(){
        if (bean != null && bean.getDetail() != null) {
            return bean.getDetail().getId();
        }
        return "";
    }

    private List<CartoonSectionInfo> getDataList(){
        if (bean != null && bean.getSection_info() != null) {
            return bean.getSection_info();
        }
        return new ArrayList<>();
    }

    private CartoonDetail getDetail() {
        if (bean != null && bean.getDetail() != null) {
            return bean.getDetail();
        }
        return new CartoonDetail();
    }


    @Override
    public CartoonEpisodeMenuAdapter.CartoonEpisodeItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_section_cartoon , parent , false);
        return new CartoonEpisodeItemViewHolder(view) ;
    }

    @Override
    public void onBindViewHolder(CartoonEpisodeMenuAdapter.CartoonEpisodeItemViewHolder holder, int position) {
        holder.setViewHolderChildViewContent(position);
    }

    @Override
    public int getItemCount() {
        if (getDataList() != null){
            return getDataList().size();
        }
        return 0;
    }

    public int getCurrentPos() {
        return currentPos;
    }

    public void setCurrentPos(int currentPos) {
        this.currentPos = currentPos;
        this.notifyDataSetChanged();
    }

    class CartoonEpisodeItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView titleTextView;
        TextView scanCountTextView;
        RelativeLayout itemLayout;
        ImageView ivLock;
        int mPosition;
        public CartoonEpisodeItemViewHolder(View itemView) {
            super(itemView);
            itemLayout = (RelativeLayout) itemView.findViewById(R.id.item_layout_id);
            ivLock = (ImageView) itemView.findViewById(R.id.ivLock);
            titleTextView = (TextView) itemView.findViewById(R.id.item_section_novel_title);
            scanCountTextView = (TextView) itemView.findViewById(R.id.item_section_scan_count);
        }

        public void setViewHolderChildViewContent(int position) {

            if (orderSec){
                mPosition = position + 1;
            }else {
                mPosition = getDataList().size() - position;
            }
            itemLayout.setOnClickListener(this);
            CartoonSectionInfo item = getDataList().get(mPosition - 1);
            if (item == null) {
                return;
            }
            titleTextView.setText(item.getTitle());
            if ("0".equals(item.getPrice())) {
                ivLock.setVisibility(View.GONE);
            }else{
                ivLock.setVisibility(View.VISIBLE);
                if(item.isNeed_pay()){
                    ivLock.setImageResource(R.drawable.img_chapter_locked_xhdpi);
                }else{
                    ivLock.setImageResource(R.drawable.img_chapter_locked_op);
                }
            }
            if (currentPos == mPosition){
                titleTextView.setTextColor(Color.parseColor("#bebebe"));
            }else {
                titleTextView.setTextColor(Color.parseColor("#666666"));
            }
            scanCountTextView.setText("浏览:" + item.getRead_times());
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.item_layout_id:
                    if (bean == null) {
                        return;
                    }
                    if ("1".equals(getDetail().getOrigin())) {
                        if (AccountService.getInstance().isLogin()) {
                            if (getDataList() != null && getDataList().size() > mPosition-1) {
                                Intent intent = new Intent();
                                intent.setClass(mContext, BrowseActivity.class);
                                intent.putExtra(Constant.URL, getDataList().get(mPosition - 1).getChapter_url());
                                intent.putExtra(Constant.TITLE, getDataList().get(mPosition - 1).getTitle());
                                intent.putExtra(Constant.JS_URL, bean.getJs_url());
                                mContext.startActivity(intent);
                            }
                        } else {
                            AccountService.getInstance().jumpToLogin();
                        }
                    }else{
                        CartoonServices.enterCartoonScanNewActivity(mContext , bean , mPosition);
                    }
                    break;
            }
        }
    }
}
